/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 15-1-22
 * Time: 上午12:58
 * To change this template use File | Settings | File Templates.
 */
if (!window.Collection) {
    if (!Array.prototype.indexOf) Array.prototype.indexOf = function (o) {
        for (var i = 0; i < this.length; i ++) {
            if (this [i] == o)
                return i;
        }
        return -1;
    };
    if (!Array.prototype.contains) Array.prototype.contains = function (o) {
        return this.indexOf (o) >= 0;
    };
    var Collection = function () {
        this.length = 0;
        this.keys = [];
    };
    Collection.prototype = {
        push : function (name, value) {
            this [name] = value;
            if (!this.keys.contains (name)) {
                this.keys.push (name);
                this.length ++;
            }
        },
        pop : function () {
            var key = this.keys.pop (),
                value = this [key];
            delete this [key];
            this.length --;
            return value;
        },
        merge : function (c) {
            if (!(c instanceof Collection)) {
                throw "Can't merge c[" + (typeof c) + ']';
            }
            var i, name;
            for (i = 0; i < c.keys.length; i ++) {
                name = c.keys [i];
                this.push (name, c [name]);
            }
        },
        isEmpty : function () {
            return !this.keys || !this.keys.length;

        },
        remove : function (name) {
            var index = this.keys.indexOf (name), i;
            for (i = index; i < this.keys.length - 1; i ++) {
                this.keys [i] = this.keys [i + 1];
            }
            this.length --;
            delete this.keys [this.keys.length - 1];
            this.keys.length --;
            delete this [name];
        },
        clear : function () {
            for (var i = 0; i < this.keys.length; i ++) {
                delete this [this.keys [i]];
            }
            this.length = 0;
            this.keys = [];
        },
        toArray : function () {
            var a = [], i = 0, name;
            for (; i < this.keys.length; i ++) {
                name = this.keys [i];
                a.push (this [name]);
            }
            return a;
        },
        toString : function () {
            var s = '', i = 0, name;
            for (; i < this.keys.length; i ++) {
                name = this.keys [i];
                if (i != 0) s += ',';
                s += name + '->' + this [name];
            }
            return s;
        },
        dispose : function () {
            this.clear ();
            delete this.keys;
            delete this.length;
        }
    };
}